
package com.android.fitnesscenter.view.client;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.text.TextUtils;

import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

public class ClientBaseActivity extends BaseActivity {

    protected static final int FROM_CLIENT_TOP_UP_ACTIVITY = 0x0001;
    protected static final int FROM_CLIENT_PAY_ACTIVITY = 0x0002;
    protected static final int FROM_CLIENT_MAIN_TOP_UP_ACTIVITY = 0x0003;
    protected static final int FROM_CLIENT_MAIN_PAY_ACTIVITY = 0x0004;

    @Override
    protected void titlebarMethod(int id) {

    }

    protected void updateMoney(final int fromActivity, String id) {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("token", getToken());
        params.addBodyParameter("imei", getIMEI());
        params.addBodyParameter("id", id);
        String url = ConfigProperties.SERVICE_URL + "updateMoney.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                if (mDialog == null) {
                    mDialog = new ProgressDialog(ClientBaseActivity.this);
                }
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (ClientBaseActivity.this != null && !ClientBaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    if (fromActivity == FROM_CLIENT_TOP_UP_ACTIVITY) {
                                        ToastHelper.getInstance().showShortMsg("充值成功!");
                                    } else if (fromActivity == FROM_CLIENT_PAY_ACTIVITY) {
                                        ToastHelper.getInstance().showShortMsg("扣款成功!");
                                    }
                                    setResult(0x0001);
                                    finish();
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(ClientBaseActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(ClientBaseActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (ClientBaseActivity.this != null && !ClientBaseActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(ClientBaseActivity.this).showErrorDialog(error);
                }
            }
        });

    }

}
